package br.ucb.cm.config.page.board.ui;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;
import java.awt.Dimension;

import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import br.ucb.cm.board.ui.ThumbBoard;
import br.ucb.cm.config.bean.BoardConfig;
import br.ucb.cm.config.page.board.ui.IfcBoardPage.ActionEnum;
import javax.swing.BoxLayout;

public class BoardPageUI extends JPanel {
	private static final long serialVersionUID = 1L;
	private IfcBoardPage controller;
	private ThumbBoard thumbnailPanel;
	private JPanel panelVisualizacao;
	
	public BoardPageUI(IfcBoardPage controller) {
		setMinimumSize(new Dimension(300, 200));
		setController(controller);
		initComponents();
	}

	private void initComponents() {
		
		JPanel panelTamanho = new JPanel();
		panelTamanho.setBorder(new TitledBorder(null, "Dimens\u00F5es", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		panelVisualizacao = new JPanel();
		panelVisualizacao.setBorder(new TitledBorder(null, "Visualiza\u00E7\u00E3o", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		final GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelVisualizacao, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelTamanho, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(159, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelTamanho, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panelVisualizacao, GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
					.addContainerGap())
		);
		panelVisualizacao.setLayout(new BoxLayout(panelVisualizacao, BoxLayout.Y_AXIS));
		
		thumbnailPanel = new ThumbBoard(2, 2, null);
		panelVisualizacao.add(thumbnailPanel);
		panelTamanho.setLayout(null);
		
		final JSpinner widthSpinner = new JSpinner();
		final JSpinner heightSpinner = new JSpinner();
		
		widthSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				Integer width = (Integer) widthSpinner.getValue();
				Integer height = (Integer) heightSpinner.getValue();
				getController().doAction(ActionEnum.CHANGE, width, height);

				thumbnailPanel.applyBoardConfig(new BoardConfig(width, height));
			}
		});
		widthSpinner.setBounds(52, 41, 38, 20);
		panelTamanho.add(widthSpinner);
		widthSpinner.setModel(new SpinnerNumberModel(2, 2, 8, 1));
		
		final JLabel lblX = new JLabel("X");
		lblX.setHorizontalAlignment(SwingConstants.CENTER);
		lblX.setBounds(98, 44, 20, 14);
		panelTamanho.add(lblX);
		
		heightSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				Integer width = (Integer) widthSpinner.getValue();
				Integer height = (Integer) heightSpinner.getValue();
				getController().doAction(ActionEnum.CHANGE, width, height);
				
				thumbnailPanel.applyBoardConfig(new BoardConfig(width, height));
			}
		});
		heightSpinner.setBounds(128, 41, 38, 20);
		panelTamanho.add(heightSpinner);
		heightSpinner.setModel(new SpinnerNumberModel(2, 2, 8, 1));
		setLayout(groupLayout);
		setVisible(true);
	}

	public IfcBoardPage getController() {
		return controller;
	}

	public void setController(IfcBoardPage controller) {
		this.controller = controller;
	}
}
